// router
import VueRouter from "vue-router";
import Kuayu from '../components/Kuayu'
import Son from '../pages/Son'
import Grandson from '../pages/Grandson'
const router = new VueRouter({
    // 默认是hash
    mode:'history',
    routes: [{
            path: '/kuayu',
            component: Kuayu
        },
        {
            path: '/son',
            component: Son,
            children: [{
                path: 'grandson',
                component: Grandson,
                // props:{id:22,name:'kaka'}
                props(route) {
                    return {
                        id: route.query.id,
                        content: route.query.content
                    }
                },
                meta: {
                    isAuth: true
                }
            }]
        }
    ]
})
router.beforeEach((to, from, next) => {
    // 原始写法，没有meta时候的写法
    // if (to.path === '/son/grandson') {
    // 这样一写之后，我们就不需要在守卫里面写if条件逻辑或之类的判断了。而是在需要判断的组件的路由定义里面写上meta对象。meta是vue定义的。meta里面的isAuth是形参
    if (to.meta.isAuth) {
        console.log('local', localStorage.getItem('key'));
        if (localStorage.getItem('key') === 'carlos') {
            next()
        } else {
            alert('没有权限！因为LocalStorage原因！')
        }
    }
    next()
})
export default router